$(document).ready(function () {

    $.each($('.close-btn'), function (i, ele) {
        $(ele).on('click', function() {
            $('.cover1').hide();
            $('.cover2').hide();
            $('.cover3').hide();
        });
    });

    var isLottery = 0, // 抽奖动作是否完毕
        hadLottery = false, // 是否抽过奖, false:没抽过
        pass = 0, // 通过验证？ 1: 通过
        prize = '价值398元避震器一个';

    var clickfunc = function() {
        var data = [1, 2, 3, 4, 5, 6];
        //data为随机出来的结果，根据概率后的结果
        data = data[Math.floor(Math.random() * data.length)];
        switch(data) {
            case 1:
                rotateFunc(1, 0, '价值398元避震器一个');
                break;
            case 2:
                rotateFunc(2, 60, '课程券');
                break;
            case 3:
                rotateFunc(3, 120, '胶手套');
                break;
            case 4:
                rotateFunc(4, 180, '运动毛巾');
                break;
            case 5:
                rotateFunc(5, 240, '谢谢参与~');
                break;
            case 6:
                rotateFunc(6, 300, '网球拍');
                break;
        }
    }

    // 抽奖按钮点击
    $('.indicator').click(function () {
        if (!pass ) { // 验证通过
            $('.cover1').show();

            return;
        } else { // 验证通过了就执行下面
            if(hadLottery) { // 抽过奖了执行
                var str = '奖品为: ' + prize;
                cover3show(str); // 提示弹窗
                isture = false;

                $('.indicator').find('img').attr('src','imgs/pointer_disable.png');
                return ;
            } else { // 没抽过奖执行
                hadLottery = true;
                clickfunc();
            }
        }

        if (isLottery) {
            return ;
        }
        isLottery = true;
    });


    function rotateFunc(awards, angle, text) {
        isLottery = true;
        $('#indi').stopRotate();
        $('#indi').rotate({
            angle: 0,
            duration: 4000, //旋转时间
            animateTo: angle + 1440, //让它根据得出来的结果加上1440度旋转
            callback: function() {
                isLottery = false; // 标志为false执行完毕
                prize = text;
                cover2show(text);
                $('.indicator').find('img').attr('src','imgs/pointer_disable.png');
            }
        });
    };


    function validate() {
        $('.to-draw-btn').trigger('click');

    }

    // “去抽奖” 按钮点击
    $('.to-draw-btn').off('click').on('click', function () {
        var phone = $('#phone').val(),
            yzmText = $('#yzmtext').val();
        if (!/^1[3|4|5|8][0-9]\d{4,8}$/.test(phone)) {
            alert('请输入正确的手机号码');
            return ;
        }
        if (yzmText != '1234') {
            alert('验证码错误');
            return ;
        }
        pass = 1;

        $('.cover1').hide();

        $('.indicator').trigger('click');
    });

    var yzmTimer = 0, // 验证码定时器
        yzmCount = 60; // 验证码倒计时

    $('.yzm-btn').on('click', function () {
        $('.yzm-btn').attr('disabled', 'disabled').html(yzmCount + 's后可重新获取');

        if (yzmCount == 0) {
            clearTimeout(yzmTimer);
            $('.yzm-btn').removeAttr('disabled').html('获取验证码');
            yzmCount = 60;
            return ;
        }
        yzmCount--;

        var thisfunc = arguments.callee;

        yzmTimer = setTimeout(function() {
            thisfunc();
        }, 1000);

    });


    function cover2show(text) {
        $('.cover2').find('.info-text2').html(text);
        $('.cover2').show();
    }

    function cover3show(text) {
        $('.cover3').find('.info-text2').html(text);
        $('.cover3').show();
    }

/* =========================================== */

    var arr = [
        { userName: '谭＊＊', goods: '运动毛巾一条', time: '2017.06.16 14:25' },
        { userName: '谭＊＊', goods: '运动毛巾一条', time: '2017.06.16 14:25'  },
        { userName: '谭＊＊', goods: '运动毛巾一条', time: '2017.06.16 14:25'  },
        { userName: '谭＊＊', goods: '运动毛巾一条', time: '2017.06.16 14:25'  }
    ]
    bannergo(arr);
    // 中奖轮播,  arr: 返回的数据
    function bannergo(arr) {
        if (arr.length <= 0) {
            return ;
        }
        // 轮播的定时器
        var timer = 0;

        $.each(arr, function (idx, item) {
            var str = '<span class="who">恭喜' + item.userName + '获得' + item.goods + '</span><span class="time">'+ item.time +'</span>';
            var oLi = $('<li class="item"/>').html(str);
            $('.draw-banner ul').append(oLi);
        });

        // 数据长度
        var len = arr.length,
            // itemH = $('.draw-banner ul').children('.item')[0].height(),
            ratio = 1/len,
            index = 0,
            oList = $('.draw-banner ul');

        timer = setInterval(function() {
            if (index == len) {
                index = 0;
            }
            oList.css('transform', 'translateY('+ -index * ratio * 100 +'%)');
            index++;

        }, 2000);
    }

});